package com.prawn.ift.ctrlequipment.service.impl;


import com.prawn.ift.ctrlequipment.dao.CtrlequipmentTypeDao;
import com.prawn.ift.ctrlequipment.pojo.CtrlequipmentStatus;
import com.prawn.ift.ctrlequipment.pojo.CtrlequipmentType;
import com.prawn.ift.ctrlequipment.service.CtrlEquipmentTypeService;
import entity.Result;
import entity.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import util.NullAttributeUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: zhy
 * @email: 1269679946@qq.com
 * @Date: 2018/10/21
 * @DESC： 控制设备类型服务实现
 */
@Service
public class CtrlEquipmentTypeServiceImpl implements CtrlEquipmentTypeService {

    @Autowired
    private CtrlequipmentTypeDao ctrlequipmentTypeDao;

    /**
     * 添加控制设备类型
     *
     * @param ctrlEquipmentType
     * @return Result
     */
    @Override
    public Result addCtrlequipmentType(CtrlequipmentType ctrlEquipmentType) {
        if (ctrlequipmentTypeDao.save(ctrlEquipmentType) != null) {
            return Result.success();
        } else {
            return Result.fail();
        }
    }

    /**
     * 删除控制设备类型
     *
     * @param id
     * @return Result
     */
    @Override
    public Result deleteCtrlequipmentType(String id) {
        ctrlequipmentTypeDao.deleteById(id);
        return Result.success();

    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result batchDelByIds( List<String> ids) {


        if(ids==null)
            return new Result(true,100,"ids为空，删除失败");
        for (String id:ids) {
            ctrlequipmentTypeDao.deleteById(id);
        }

        return new Result(true,200,"删除成功");
    }


    /**
     * 修改控制设备类型信息
     *
     * @param ctrlEquipmentType
     * @return Result
     */
    @Override
    public Result editCtrlequipmentType(CtrlequipmentType ctrlEquipmentType) {


        CtrlequipmentType oldCtrlEquipmentType = ctrlequipmentTypeDao.findById(ctrlEquipmentType.getId()).get();
        BeanUtils.copyProperties(ctrlEquipmentType, oldCtrlEquipmentType, NullAttributeUtils.getNullPropertyNames(ctrlEquipmentType));


        ctrlequipmentTypeDao.save(oldCtrlEquipmentType);
        return Result.success();

    }

    /**
     * 查询控制设备类型信息
     *
     * @param id
     * @return CtrlequipmentType
     */
    @Override
    public CtrlequipmentType getCtrlequipmentTypeById(String id) {
        return ctrlequipmentTypeDao.findById(id).get();
    }

    /**
     * 异步查询通知设备分类信息
     *
     * @param parentId
     * @return Result
     */
    @Override
    public Result getCtrlequipmentTypePid(String parentId) {

        Sort sort = new Sort(Sort.Direction.ASC, "sort");
        CtrlequipmentType ctrlequipmentType = new CtrlequipmentType();
        ctrlequipmentType.setParentId(parentId);
        Example example = Example.of(ctrlequipmentType);
        List<CtrlequipmentType> ctrlEquipmentTypeList = ctrlequipmentTypeDao.findAll(example, sort);
        Result result = new Result();
        result.setData(ctrlEquipmentTypeList);
        return result;
    }

    /**
     * 查询通知设备分类信息列表
     *
     * @return Result
     */
    @Override
    public Result getCtrlequipmentTypeList() {
        List<CtrlequipmentType> ctrlEquipmentTypeList = ctrlequipmentTypeDao.findAll(new Sort(Sort.Direction.ASC, "sort"));
        return Result.success().add(ctrlEquipmentTypeList);
    }

    /**
     * 查询通知设备分类信息列表,并生成树形数据
     *
     * @return Result
     */
    @Override
    public Result getCtrlequipmentTypeTree() {
        //获取所有分类列表
        List<CtrlequipmentType> ctrlEquipmentTypeList = (List<CtrlequipmentType>) this.getCtrlequipmentTypeList().getData();

        //获取顶级分类列表
        List<CtrlequipmentType> rootCtrlequipmentTypeList = (List<CtrlequipmentType>) this.getCtrlequipmentTypePid("0").getData();

        for (CtrlequipmentType ctrlEquipmentType : rootCtrlequipmentTypeList) {
            ctrlEquipmentType.setChildren(getChild(ctrlEquipmentType.getId(), ctrlEquipmentTypeList));
        }
        Result result = new Result();
        result.setData(rootCtrlequipmentTypeList);
        return result;
    }


    private List<CtrlequipmentType> getChild(String id, List<CtrlequipmentType> ctrlEquipmentTypeList) {
        // 子菜单
        List<CtrlequipmentType> childList = new ArrayList<>();

        // 遍历所有节点，将父菜单id与传过来的id比较
        for (CtrlequipmentType ctrlEquipmentType : ctrlEquipmentTypeList) {
            if (ctrlEquipmentType.getParentId() != null) {   //所有非以及菜单的都要加入chidList中（递归）
                if (ctrlEquipmentType.getParentId().equals(id)) {
                    childList.add(ctrlEquipmentType);
                }
            }
        }

        // 把子菜单的子菜单再循环一遍
        for (CtrlequipmentType ctrlEquipmentType : childList) {// 没有url子菜单还有子菜单（暂不考虑url的问题）
            // 递归
            ctrlEquipmentType.setChildren(getChild(ctrlEquipmentType.getId(), ctrlEquipmentTypeList));
        }
        // 递归退出条件
        if (childList.size() == 0) {
            return null;
        }
        return childList;
    }
}
